<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 11.5</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="11.4.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="11.6.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P2">Part II. Tables and Objects</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#11">Chapter 11. Data Structures</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><h2>11.5 &ndash; Sets and Bags</h2>

<p>Suppose you want to list all identifiers used in a program source;
somehow you need to filter the reserved words out of your listing.
Some C programmers could be tempted to represent the set of
reserved words as an array of strings,
and then to search this array to
know whether a given word is in the set.
To speed up the search,
they could even use a binary tree or a hash table to represent the set.

<p>In Lua, an efficient and simple way to represent such sets is
to put the set elements as <em>indices</em> in a table.
Then, instead of searching the table for a given element,
you just index the table and test whether the result is <B>nil</B> or not.
In our example, we could write the next code:
<pre>
    reserved = {
      ["while"] = true,     ["end"] = true,
      ["function"] = true,  ["local"] = true,
    }
    
    for w in allwords() do
      if reserved[w] then
        -- `w' is a reserved word
        ...
</pre>
(Because <b>while</b> is a reserved word in Lua,
we cannot use it as an identifier.
Therefore, we cannot write <code>while = 1</code>;
instead, we use the <code>["while"] = 1</code>  notation.)

<p>You can have a clearer initialization using an auxiliary function to
build the set:
<pre>
    function Set (list)
      local set = {}
      for _, l in ipairs(list) do set[l] = true end
      return set
    end
    
    reserved = Set{"while", "end", "function", "local", }
</pre>

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="11.6.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

